/*
 * Copyright (c) 2009 LastFM.Desktop Anton Pavlik.
 * All Rights Reserved.
 * 
 * You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, 
 * with or without modifications, and in Source or Object form, provided that You meet the following 
 * conditions:
 * 1. You must give any other recipients of the Work or Derivative Works a copy of this License; and
 *
 * 2. You must cause any modified files to carry prominent notices stating that You changed the files; 
 * and
 *
 * 3. You must retain, in the Source form of any Derivative Works that You distribute, all copyright, 
 * patent, trademark, and attribution notices from the Source form of the Work, excluding those notices 
 * that do not pertain to any part of the Derivative Works.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * You may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, Licensor provides the Work 
 * (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
 * CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties 
 * or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. 
 * YOU ARE SOLELY RESPONSIBLE FOR DETERMINING THE APPROPRIATENESS OF USING OR REDISTRIBUTING THE WORK
 * AND ASSUME ANY RISKS ASSOCIATED WITH YOUR EXERCISE OF PERMISSIONS UNDER THIS LICENSE. 
 */
package org.shake.lastfm.ui.config;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JToggleButton;

import org.jdesktop.swingx.JXPanel;
import org.shake.lastfm.Resources;
import org.shake.lastfm.model.UserPreferences;

import com.jgoodies.forms.builder.ButtonBarBuilder2;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.layout.FormLayout;

public class PreferencesDialog extends JDialog
{
    private static final long serialVersionUID = 6258326941514607364L;

    private JXPanel contentPanel;
    private CardLayout contentLayout;

    private JPasswordField passwdFld;
    private JTextField loginFld;
    
    private final static String NETWORK_PAGE = "network";
    private final static String GENERAL_PAGE = "general";
    
    public PreferencesDialog(JFrame parent)
    {
        super(parent);
        
        buildUI();
        setSize(400, 400);

        loadPreferences();
    }

    private void buildUI()
    {
        setDefaultLookAndFeelDecorated(false);
        
        this.contentLayout = new CardLayout();
        this.contentPanel = new JXPanel(this.contentLayout);
        this.contentPanel.add( buildNetworkConfigPanel(), NETWORK_PAGE );
        this.contentPanel.add( buildGeneralConfigPanel(), GENERAL_PAGE );
        
        setLayout(new BorderLayout());
        getContentPane().add(this.contentPanel, BorderLayout.CENTER);
        getContentPane().add(buildToolsPanel(), BorderLayout.NORTH);
        getContentPane().add(buildButtonsBar(), BorderLayout.SOUTH);
    }
    
    private JComponent buildToolsPanel()
    {
        AbstractButton credentialsBtn = createToolButton(new CredentialsAction());
        AbstractButton configBtn = createToolButton(new ConfigAction());
        
        ButtonGroup group = new ButtonGroup();
        group.add(credentialsBtn);
        group.add(configBtn);

        JXPanel result = new JXPanel();
        ButtonBarBuilder2 builder = new ButtonBarBuilder2(result);
        builder.addButton(configBtn, credentialsBtn);
        
        credentialsBtn.doClick();
        return result;
    }
    
    private AbstractButton createToolButton(Action action)
    {
        JToggleButton result = new JToggleButton(action);
        result.setRolloverEnabled(true);
        result.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        result.setVerticalTextPosition(AbstractButton.BOTTOM);
        result.setHorizontalTextPosition(AbstractButton.CENTER);
        
        return result;
    }
    
    private JComponent buildNetworkConfigPanel()
    {
        JXPanel result = new JXPanel();
        result.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        
        this.loginFld = new JTextField();
        this.passwdFld = new JPasswordField();
        
        FormLayout layout = new FormLayout("10dlu, p, 3dlu, p:grow");
        DefaultFormBuilder builder = new DefaultFormBuilder(layout, result);
        builder.setLeadingColumnOffset(1);
        
        builder.appendSeparator("Last.fm user");
        builder.append("Login", this.loginFld);
        builder.append("Password", this.passwdFld);
        
        return result;
    }
    
    private void loadPreferences()
    {
        UserPreferences pref = UserPreferences.getInstance();
        this.loginFld.setText(pref.getUserName());
        this.passwdFld.setText(pref.getPasswd());
    }

    private JComponent buildGeneralConfigPanel()
    {
        JXPanel result = new JXPanel();
        
        return result;
    }
    
    private JComponent buildButtonsBar()
    {
        JXPanel result = new JXPanel();
        ButtonBarBuilder2 builder = new ButtonBarBuilder2(result);
        
        builder.addGlue();
        builder.addButton(new ApplyPreferencesAction());
        builder.addRelatedGap();
        builder.addButton(new CloseAction());

        result.setBorder(Borders.DIALOG_BORDER);
        
        return result;
    }
    
    private class CredentialsAction extends AbstractAction
    {
        private static final long serialVersionUID = -207106783950335174L;

        public CredentialsAction()
        {
            super("Network", Resources.getIcon("image.preferences.network.32x32"));
        }

        @Override
        public void actionPerformed(ActionEvent e)
        {
            PreferencesDialog.this.contentLayout.show(PreferencesDialog.this.contentPanel, NETWORK_PAGE);            
        }
    }
    
    private class ConfigAction extends AbstractAction
    {
        private static final long serialVersionUID = 8890618591903500596L;

        public ConfigAction()
        {
            super("General", Resources.getIcon("image.preferences.general.32x32"));
        }

        @Override
        public void actionPerformed(ActionEvent e)
        {
            PreferencesDialog.this.contentLayout.show(PreferencesDialog.this.contentPanel, GENERAL_PAGE);            
        }
    }
    
    private class ApplyPreferencesAction extends AbstractAction
    {
        private static final long serialVersionUID = 1468859049618148248L;

        public ApplyPreferencesAction()
        {
            super("Apply");
        }
        
        @Override
        public void actionPerformed(ActionEvent e)
        {
            UserPreferences.getInstance().setPasswd(PreferencesDialog.this.passwdFld.getPassword());
            UserPreferences.getInstance().setUserName(PreferencesDialog.this.loginFld.getText());
            UserPreferences.getInstance().save();
        }
    }
    
    private class CloseAction extends AbstractAction
    {
        private static final long serialVersionUID = 8426950002796868790L;
        
        public CloseAction()
        {
            super("Close");
        }

        @Override
        public void actionPerformed(ActionEvent e)
        {
            PreferencesDialog.this.setVisible(false);
            PreferencesDialog.this.dispose();
        }
    }
}